home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / misc / vpan100.zip / VPGRAPH.H < prev    next >
C/C++ Source or Header  |  1995-01-18  |  12KB  |  367 lines

  1. //
  2. //  VIRTUAL PANELS * GRAPHIC USER UNTERFACE FOR LABORATORY WORKS
  3. //
  4. //        VPGRAPH.H : CLASS: Graph
  5. //
  6. //                       |     Written by O.Rasizade
  7. //    declarations       |        1993
  8. //                       |
  9. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  10.  
  11. #ifndef __cplusplus
  12. #error Must use C++ 
  13. #endif
  14.  
  15. #ifndef __VPGRAPH_H
  16. #define __VPGRAPH_H
  17.  
  18.  
  19. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  20. //++++++++++++++ CLASS   << _Graph >>  +++++++++++++++
  21. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22.  
  23. #define  XNOTCHLENGTH    5    // for graph X axis tic marks
  24. #define  YNOTCHLENGTH    5    // for graph Y axis tic marks.
  25.  
  26. #define     LIN    0    // type of scale
  27. #define     LOG    1
  28.  
  29. struct graphcolors { COLORS
  30.             Paper,
  31.             Ink,
  32.             Grid,
  33.             Title,
  34.             AxesTitles,
  35.             TicLabels; };
  36.  
  37. //--------------- Color schemes -------------
  38. const graphcolors WhiteOnCyan={
  39.         CYAN,        //paper color
  40.         WHITE,        //Ink
  41.         LIGHTCYAN,    //Grid2Col
  42.         RED,        //Title
  43.         BLACK,        //AxesTitles
  44.         BLUE         //TicLabels
  45.         };
  46. const graphcolors YellowOnCyan={
  47.         CYAN,        //paper color
  48.         YELLOW,        //Ink
  49.         LIGHTCYAN,    //Grid2Col
  50.         RED,        //Title
  51.         BLACK,        //AxesTitles
  52.         BLUE         //TicLabels
  53.         };
  54.  
  55. const graphcolors GreenOnBlue={
  56.         BLUE,        //paper color
  57.         LIGHTGREEN,    //Ink color
  58.         LIGHTBLUE,    //Grid2Col
  59.         RED,        //Title
  60.         BLACK,        //AxesTitles
  61.         BLUE        //TicLabels
  62.         };
  63.  
  64. const graphcolors YellowOnBlue={
  65.         BLUE,        //paper color
  66.         YELLOW,        //Ink color
  67.         LIGHTBLUE,    //Grid2Col
  68.         RED,        //Title
  69.         BLACK,        //AxesTitles
  70.         BLUE        //TicLabels
  71.         };
  72.  
  73. class _Graph : public display, public object
  74. {
  75.  protected:
  76.  
  77.     int   xxlenp, yylenp,
  78.           numbheight,
  79.           yymaxpd,    // relative coord.of paper in frame
  80.  
  81.           xtics, ytics,        // number of tic marks
  82.           xpixtic,ypixtic,    // number of pixels between tics
  83.           xscale,yscale;    // LOG or LIN - type of scale
  84.  
  85.     float xval0,yval0,    /* values of x,y at origin        */
  86.           xticinc,yticinc,    // increment ofvalue of tic labels
  87.                 //  only for LIN scales
  88.           xvalmax,yvalmax,    /* maximal values of x,y        */
  89.           xpixval,ypixval;    /* pixels per unit for x,y        */
  90.  
  91.     char *xtitle, *ytitle,    /* axises titles            */
  92.          *xticfmt,*yticfmt;    /* formats of tic labels        */
  93.     //fonts and their sizes
  94.     int axtitlefont,axtitlefontsize,    /*  for axises titles    */
  95.         ticfont, ticfontsize;    /*  and tic labels        */
  96.  
  97.        plaquecolors framecolors;
  98.        graphcolors grcolors;      // disp colors are inherited
  99.        int gridstyle;        // user defined line style
  100.  
  101.        int xx0ticlab, xxmaxticlab,yvalwidth;
  102.  
  103.  public:
  104.  
  105.  //------ Constructor ---- ALL SIZES IN VGA PIXELS ------
  106. _Graph(
  107.     int _x0,int _y0,    /* where to paint graph*/
  108.     objtype _type,        // may be FIXED,PERM,POPUP
  109.     char *_title,
  110.     int xtics,int ytics,    /* number of x & y div.            */
  111.     int xpixtic,int ypixtic,// number of pixels between tics
  112.  
  113.     float _xval0,float _yval0,/* values of x,y (LIN) or powers of 10 (LOG) at origin*/
  114.     float _xvalmax,float _yvalmax,// values of x,y at end of axis
  115.                       //  only for LIN scales
  116.               //------------ Hereafter are defaults
  117.     void (far *_paintproc)()=procNULL,
  118.     char *xtitle="",char *ytitle="",    // axes titles
  119.     char *xticfmt="",char *yticfmt="", // format of tic labels
  120.                        // can be only float %[..]f
  121.     int _xscale=LIN, int _yscale=LIN,  // may be LIN or LOG
  122.     int _frame=FRAMED,            // display may FRAMED or UNFRAMED
  123.     int _titlefont=0,int _titlefontsize=1,    // title
  124.     int axtitlefont=0,int axtitlefontsize=1,      // tic labels
  125.     int ticfont=SMALL_FONT,int ticfontsize=2,    //div.numbers
  126.  
  127.     graphcolors graphcolconfig=YellowOnCyan,
  128.     plaquecolors framecolconfig=plaquecoldflt,
  129.     int gridstyle=0x5555);        // style of grid lines
  130.     //---------------------- end of constructor declaration
  131.  
  132.     void SetupAxisX(
  133.          float _xval0,// value of x (LIN) or powers of 10 (LOG) at origin
  134.          float _xvalmax);// values of x at end of axis(only for LIN)
  135.  
  136.     void SetupAxisY(
  137.          float _yval0,// value of y (LIN) or powers of 10 (LOG) at origin
  138.          float _yvalmax);// values of y at end of axis(only for LIN)
  139.  
  140.     void DrawGrid(void);
  141.     void PrintTicLabelsX(void);
  142.     void PrintTicLabelsY(void);
  143. virtual void Paint(void);
  144. virtual void cls();
  145.     void SetInk(COLORS inkcolor)
  146.         {grcolors.Ink=inkcolor;}
  147.  
  148. // --- IF color=-1 THE USED CURRENT graphcolors.Ink COLOR.
  149.     //------- DOT & LINE -------
  150. void PutDot( double x, double y, int color=-1);
  151. void PutLine( double x0,double y0,double x1,double y1, int color=-1);
  152.  
  153. };//++++++++++++++ END of class _Graph ++++++++++++++++++
  154.  
  155.  
  156. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  157. //++++++++++++++ CLASS TEMPLATE  << Graph >>  +++++++++++++++
  158. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  159.  
  160. template<class XType,class YType>
  161. class Graph : public _Graph
  162. {
  163.  public:
  164.  
  165.  //------ Constructor ---- ALL SIZES IN VGA PIXELS ------
  166. Graph(
  167.     int _x0,int _y0,    /* where to paint graph*/
  168.     objtype _type,        // may be FIXED,PERM,POPUP
  169.     char *_title,
  170.     int xtics,int ytics,    /* number of x & y div.            */
  171.     int xpixtic,int ypixtic,// number of pixels between tics
  172.  
  173.     float _xval0,float _yval0,/* values of x,y (LIN) or powers of 10 (LOG) at origin*/
  174.     float _xvalmax,float _yvalmax,// values of x,y at end of axis
  175.                       //  only for LIN scales
  176.               //------------ Hereafter are defaults
  177.     void (far *_paintproc)()=procNULL,
  178.     char *xtitle="",char *ytitle="",    // axes titles
  179.     char *xticfmt="",char *yticfmt="", // format of tic labels
  180.                        // can be only float %[..]f
  181.     int _xscale=LIN, int _yscale=LIN,  // may be LIN or LOG
  182.     int _frame=FRAMED,            // display may FRAMED or UNFRAMED
  183.     int _titlefont=0,int _titlefontsize=1,    // title
  184.     int axtitlefont=0,int axtitlefontsize=1,      // tic labels
  185.     int ticfont=SMALL_FONT,int ticfontsize=2,    //div.numbers
  186.  
  187.     graphcolors graphcolconfig=YellowOnCyan,
  188.     plaquecolors framecolconfig=plaquecoldflt,
  189.     int gridstyle=0x5555) :        // style of grid lines
  190.  
  191.       _Graph(_x0,_y0,_type,_title,xtics,ytics,xpixtic,ypixtic,_xval0,_yval0,
  192.     _xvalmax,_yvalmax,_paintproc,xtitle,ytitle,xticfmt,yticfmt,
  193.     _xscale,_yscale,_frame,_titlefont,_titlefontsize,
  194.     axtitlefont,axtitlefontsize,ticfont,ticfontsize,
  195.     graphcolconfig,framecolconfig,gridstyle)
  196.     {}
  197. //---------------------- end of constructor Graph::Graph
  198.  
  199. // --- IF color=-1 THE USED CURRENT graphcolors.Ink COLOR.
  200.  
  201.  
  202.     //------- ARRAY ----
  203. void  ShowCurve( int numpoints, XType x0c, XType xmaxc,
  204.                     YType far *curve,int color=-1);
  205. void  HideCurve(int numpoints,XType x0c,XType xmaxc,YType far *curve)
  206.         { ShowCurve(numpoints,x0c,xmaxc,curve,grcolors.Paper);DrawGrid();}
  207.  
  208. void  ShowHistogram(int numpoints,XType x0a,XType xstep,
  209.                 YType far *array, int color=-1);
  210.                     // if color=-1 then it is default
  211. void  HideHistogram(int numpoints,XType x0a,XType xstep,
  212.                             YType far *array)
  213.     { ShowHistogram(numpoints,x0a,xstep,array,grcolors.Paper);
  214.       DrawGrid();}
  215.  
  216.  
  217.     //------- TWO ARRAYS ----
  218.  void  ShowCurve(int numpoints, XType far *Xdata,
  219.                       YType far *Ydata, int color);
  220.  void  HideCurve(int numpoints, XType far *Xdata, YType far *Ydata)
  221.         { ShowCurve(numpoints, Xdata, Ydata, grcolors.Paper);DrawGrid();}
  222.  
  223.     //------- FUNCTION ----
  224. void  ShowCurve(int numpoints,YType (far *func)(XType ),int color=-1);
  225. void  HideCurve(int numpoints,YType (far *func)(XType ))
  226.         { ShowCurve(numpoints,func,grcolors.Paper);DrawGrid();}
  227.  
  228. };//++++++++++++++ END of class Graph ++++++++++++++++++
  229.  
  230.  
  231. //---------- TEMPLATES OF MEMBER FUNCTIONS --------
  232.  
  233. template<class XType,class YType>
  234. void  Graph<XType,YType>::ShowCurve(int numpoints,XType x0c,XType xmaxc,
  235.                 YType far *curve, int color)
  236.                     // if color=-1 then it is default
  237. {
  238.  int i;
  239.  double dxc, xi, yi;
  240.  
  241.  setviewport(xx0p,yy0p,xxmaxp,yymaxp,CLIP_ON);
  242.  setcolor((color==-1)?grcolors.Ink:color);
  243.  setlinestyle(SOLID_LINE,0,NORM_WIDTH);
  244.  
  245.  dxc=(xmaxc-x0c)/(numpoints-1);
  246.  xi = (xscale == LOG) ? log10(x0c) : x0c;
  247.  yi = curve[0];
  248.  if ( yscale == LOG )  yi = ( yi == 0 ) ? yval0 : log10(yi);
  249.  moveto( round( xpixval*( xi - xval0) ),
  250.       round( ypixval*( yvalmax - yi) ));
  251.  for (i=1;i<numpoints;i++)
  252.   {
  253.     xi = x0c + dxc * i;
  254.     yi = curve[i];
  255.     if ( xscale == LOG )  xi = log10(xi);
  256.     if ( yscale == LOG )  yi = ( yi == 0 ) ? yval0 : log10(yi);
  257.     lineto(  round( xpixval*( xi - xval0 )),
  258.          round( ypixval*( yvalmax - yi ) ) );
  259.   }// for
  260. }//-------- END of Graph::ShowCurve(,,,YType *) -------------
  261.  
  262. template<class XType,class YType>
  263. void  Graph<XType,YType>::ShowHistogram(int numpoints,
  264.                 XType x0a,XType xstep,
  265.                 YType far *array, int color)
  266.                     // if color=-1 then it is default
  267. {
  268.  int i, BAR, xi, ybar0, ybar1;
  269.  double xstep2, xbar0, xbar1;
  270.  
  271.  setviewport(xx0p,yy0p,xxmaxp,yymaxp,CLIP_ON);
  272.  if ( xscale == LOG )
  273.     lgErrExit(" Graph::ShowHistogram : X-scale cannot be LOG for histogram");
  274.  xstep2 = xstep/2.;
  275.  xbar0 = x0a-xval0;
  276.  xbar1 = xbar0+xstep2;
  277.  xbar0 -= xstep2;
  278.  
  279.  setfillstyle( SOLID_FILL, (color==-1)?grcolors.Ink:color);
  280.  setcolor( (color==-1)? grcolors.Ink : color );
  281.  BAR = ( (xpixval*(xbar1 - xbar0)) < 5 ) ? 0 : 1;
  282.  for ( i=0; i<numpoints; i++)
  283.     {
  284.     double arrayi = array[i];
  285.      if ( yscale == LIN )
  286.       {                // y-LIN
  287.        arrayi = array[i];
  288.        ybar0=( arrayi > 0 ) ?  round( ypixval* (yvalmax - arrayi))
  289.                  : round( ypixval * yvalmax);
  290.        ybar1=( arrayi < 0 ) ?  round( ypixval * (yvalmax - arrayi))
  291.                  : round( ypixval * yvalmax);
  292.        }
  293.      else               // y-LOG
  294.       {
  295.        ybar0 = ( arrayi == 0 ) ? yylenp :
  296.                 round( ypixval* (yvalmax - log10(array[i])));
  297.        ybar1 = yylenp;
  298.        }
  299.      if ( BAR )
  300.        bar( round( xpixval*(xbar0 + xstep*i)) + 2, ybar0,
  301.         round(xpixval*(xbar1 + xstep*i)) - 2, ybar1);
  302.      else
  303.       {xi =  round( xpixval*(x0a - xval0 + xstep*i));
  304.        line( xi, ybar0, xi, ybar1);}
  305.     } /* for */
  306.  
  307. }//-------- END of Graph::ShowHistogram(,,,YType*) ----
  308.  
  309. template<class XType,class YType>
  310. void  Graph<XType,YType>::ShowCurve(int numpoints,
  311.                       XType far *Xdata,
  312.                       YType far *Ydata, int color)
  313. {                    // if color=-1 then it is default
  314. int i;
  315.  double xi, yi;
  316.  
  317.  setviewport(xx0p,yy0p,xxmaxp,yymaxp,CLIP_ON);
  318.  setcolor((color==-1)?grcolors.Ink:color);
  319.  
  320.  xi = Xdata[0];
  321.  if ( xscale == LOG )  xi = ( xi == 0 ) ? xval0 : log10(xi);
  322.  yi = Ydata[0];
  323.  if ( yscale == LOG )  yi = ( yi == 0 ) ? yval0 : log10(yi);
  324.  moveto(  round( xpixval*( xi - xval0)),
  325.       round( ypixval*( yvalmax - yi) ));
  326.  for (i=1;i<numpoints;i++)
  327.   {
  328.     xi = Xdata[i];
  329.     yi = Ydata[i];
  330.     if ( xscale == LOG )  xi = log10(xi);
  331.     if ( yscale == LOG )  yi = ( yi == 0 ) ? yval0 : log10(yi);
  332.     lineto(  round( xpixval*( xi - xval0 )),
  333.          round( ypixval*( yvalmax - yi )) );
  334.   }// for
  335. }//-------- END of Graph::ShowCurve(,,,XType *x,YType *y) -------------
  336.  
  337.  
  338. template<class XType,class YType>
  339. void  Graph<XType,YType>::ShowCurve(int numpoints,
  340.                       YType (far *func)(XType),
  341.                       int color)
  342. {                    // if color=-1 then it is default
  343. int i;
  344. double  xi, yi, dxc, xval0c, xvalmaxc ;
  345.  
  346.  setviewport(xx0p,yy0p,xxmaxp,yymaxp,CLIP_ON);
  347.  setcolor((color==-1)?grcolors.Ink:color);
  348.  if ( xscale == LOG )
  349.     {xval0c = pow10(xval0);    xvalmaxc = pow10(xvalmax); }
  350.  else   {xval0c = xval0;    xvalmaxc = xvalmax; }
  351.  dxc = ( xvalmaxc - xval0c ) / (numpoints-1) ;
  352.  yi = (*func)( xval0c );
  353.  if ( yscale == LOG )     yi = ( yi == 0 ) ? yval0 : log10( yi ) ;
  354.  moveto( 0,  round( ypixval * ( yvalmax - yi ) ) );
  355.  
  356.  for (i=1;i<numpoints;i++)
  357.     {xi = xval0c + dxc*i;
  358.      yi = (*func)( xi );
  359.      if ( xscale == LOG )  xi = log10(xi);
  360.      if ( yscale == LOG )  yi = ( yi == 0 ) ? yval0 : log10( yi ) ;
  361.      lineto(  round( xpixval * (xi - xval0) ),
  362.           round( ypixval * (yvalmax - yi)) );
  363.      }
  364. }//--------- END of _Graph::ShowCurve(,,, *()) ---------------
  365.  
  366. #endif  // __VPGRAPH_H
  367.